package camera
{
    import away3d.cameras.Camera3D;
    import away3d.tools.utils.Bounds;
    
    import environment.Environment;
    
    import flash.display.Stage;
    import flash.display.Stage3D;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.geom.Vector3D;

    public class EnvironmentCameraController
    {
        protected var stage :Stage;
        
        protected var camer : Camera3D;
        
        protected var loc : Vector3D;
        
        protected var env :Environment;
        
        protected var minX : Number = 0;
        protected var maxX : Number = 0;
        protected var minY : Number = 0;
        protected var maxY : Number = 0;
        protected var minZ : Number = 0;
        protected var maxZ : Number = 0;
        
        protected var side : int = 0;
        
        protected var moveDir : int;
        
        protected var moveSpeed : Number = 50;
        
        public function EnvironmentCameraController(cam : Camera3D, stage : Stage, env :Environment, side : int = 0)
        {
            this.stage = stage;
            this.camer = cam;
            this.env = env;
            this.side = side;
            
            cam.lens.near = 0.5;
            cam.lens.far = 5000;
            
            camer.x = env.worldWidth / 2;
            camer.y = 0;
            
            camer.z = -1500;
            
            minX = 0;
            minY = 0;
            maxX = env.worldWidth;
            maxY = env.worldHeight;
            
            minZ = -1800;
            maxZ = 500;
            stage.addEventListener(Event.ENTER_FRAME, stage_enterFrameHandler);
            stage.addEventListener(MouseEvent.MOUSE_MOVE, stage_mouseMoveHandler);
            stage.addEventListener(MouseEvent.MOUSE_WHEEL, stage_mouseWheelHandler);
        }
        
        protected function stage_mouseWheelHandler(event :MouseEvent) : void
        {
            camer.z += event.delta * 100;
            
            if (camer.z < minZ)
            {
                camer.z = minZ;
            }
            else if (camer.z > maxZ)
            {
                camer.z = maxZ;
            }
            
            camer.lookAt(new Vector3D(env.worldWidth / 2, env.worldHeight / 2, 0), new Vector3D(0,0,-1));
        }
        
        protected function stage_mouseMoveHandler(event :MouseEvent) : void
        {
            if (side == 0)
            {
                moveDir = 0x0;
            
                if (event.stageX < 50)
                {
                    moveDir |= 0x1;
                }
                else if (event.stageX > stage.width - 50)
                {
                    moveDir |= 0x2;
                }
                
                if (event.stageY < 50)
                {
                    moveDir |= 0x4;
                }
                else if (event.stageY > stage.height - 50)
                {
                    moveDir |= 0x8;
                }
            }    
        }
        protected function stage_enterFrameHandler(event :Event) : void
        {
            // LEFT
            if (moveDir & 0x1)
            {
                camer.x -= moveSpeed;
            }
            else if (moveDir & 0x2)
            {
                camer.x += moveSpeed;
            }
            
            if (moveDir & 0x4)
            {
                camer.y += moveSpeed;
            }
            else if (moveDir & 0x8)
            {
                camer.y -= moveSpeed;
            }
            
            if (camer.x < minX)
            {
                camer.x = minX;
            }
            if (camer.x > maxX)
            {
                camer.x = maxX;
            }
            if (camer.y < minY)
            {
                camer.y = minY;
            }
            if (camer.y > maxY)
            {
                camer.y = maxY;
            }
            
            camer.lookAt(new Vector3D(env.worldWidth / 2, env.worldHeight / 2, 0), new Vector3D(0,0,-1));
        }
    }
}